$(function () {

    $("#tab-2").css( 'width', $("#tab-1").width() );
    $("#tab-4").css( 'width', $("#tab-3").width() );
    $("#tab-5").css( 'width', $("#tab-3").width() );
    var communityId=$('#communityId').val();
       $.ajax({
           url:'/estate/priceData',
           data:{'communityId':communityId},
           success:function(data){
               var obj=data.communityPrices;
              for(var i=0,price=[],districtPrice=[],cityPrice=[],statsDate=[];i<obj.length;i++){
                   price.push(obj[i]['price']);
                  districtPrice.push(obj[i]['districtPrice']);
                  cityPrice.push(obj[i]['cityPrice']);
                  statsDate.push(obj[i]['statsDate']);
              }
               var myChart1 = echarts.init($("#tab-1").get(0));
               option1 = {
                   tooltip: {
                       trigger: 'axis'
                   },
                   legend: {
                       data:['当前价格','区域价格','城市价格']
                   },
                   grid: {
                       left: '3%',
                       right: '3%',
                       bottom: '6%',
                       containLabel: true
                   },
                   xAxis: {
                       type: 'category',
                       boundaryGap: false,
                       axisLabel: {
                           interval:0,
                           rotate:20
                       },
                       data: statsDate
                   },
                   yAxis: {
                       type: 'value'
                   },
                   series: [
                       {
                           name:'当前价格',
                           type:'line',
                           smooth:true,
                           data:price
                       },
                       {
                           name:'区域价格',
                           type:'line',
                           smooth:true,
                           data:districtPrice
                       },
                       {
                           name:'城市价格',
                           type:'line',
                           smooth:true,
                           data:cityPrice
                       }
                   ]
               };
               myChart1.setOption(option1);
               $(window).resize(function() {
                   //重置容器高宽
                   myChart1.resize;
               });
           }
       })

       $.ajax({
           url:'/estate/ratioData',
           data:{'communityId':communityId},
           success:function(data){
               var obj=data.communityRentalRatios;
               for(var i=0,ratio=[],statsDate=[];i<obj.length;i++){
                   ratio.push(obj[i]['ratio']);
                   statsDate.push(obj[i]['statsDate']);
               }
           var myChart2 = echarts.init($("#tab-2").get(0));
    option2 = {
        title: {
            text: ''
        },
        tooltip: {
            trigger: 'axis'
        },
        legend: {
            data:['租售比']
        },
        grid: {
            left: '6%',
            right: '3%',
            bottom: '6%',
            containLabel: true
        },
        xAxis: {
            type: 'category',
            boundaryGap: false,
            axisLabel: {
                interval:0,
                rotate:20
            },
            data: statsDate
        },
        yAxis: {
            type: 'value'
        },
        series: [
            {
                name:'租售比',
                type:'line',
                smooth:true,
                data:ratio
            }
        ]
    };
    myChart2.setOption(option2);
    $(window).resize(function(){
        myChart2.resize;
    })
           }
       })

    // 交易活跃度热力图
    var map=new BMap.Map("tab-3", {minZoom:13,maxZoom:18,enableMapClick:false});
    map.centerAndZoom(new BMap.Point(120.216087,30.25458),13);
    // map.centerAndZoom(cityName.value, 13);// 初始化地图，设置中心点坐标和地图级别
    // var marker = new BMap.Marker(cityName.value);  // 创建标注
    // map.addOverlay(marker);               // 将标注添加到地图中
    map.enableScrollWheelZoom();
    var heatmapOverlay;
    var zoneNumber = $("#city").val();
    var cityName = $('#cityName').val();
    function initMap(zoneNumber,cityName) {
        map.centerAndZoom(cityName, 13);// 初始化地图，设置中心点坐标和地图级别
    $.ajax({
        url:"/map/heatData",
        data:{"city":zoneNumber},
        dataType:"json",
        type:"post",
        success:function (result) {
            if(result.result.length==0){
                return
            }
            var points = result.result;
            heatmapOverlay = new BMapLib.HeatmapOverlay({"radius":20});
            map.addOverlay(heatmapOverlay);
            heatmapOverlay.setDataSet({data:points,max:10});
        }
    })
    }
    initMap(zoneNumber,cityName)
    //成交周期Homer
    var myChart4 = echarts.init($("#tab-4").get(0));
    option4 = {
        tooltip : {
            formatter: "{a} <br/>{b} : {c}%"
        },
        series: [
            {
                name: '成交周期',
                type: 'gauge',
                detail: {formatter:'{value}天'},
                data: [{value: $('#dealCycle').text(), name: '成交周期'}]
            }
        ]
    };
    myChart4.setOption(option4);
    $(window).resize(function(){
        myChart4.resize;
    })
    //挂牌量与成交量
    $.ajax({
        url:'/estate/saleData',
        data:{'communityId':communityId},
        success:function(data){
            var obj=data.communityQuantityRisks;
            for(var i=0,netNumber=[],dealNumber=[],statsDate=[];i<obj.length;i++){
                netNumber.push(obj[i]['netNumber']);
                dealNumber.push(obj[i]['dealNumber']);
                statsDate.push(obj[i]['statsDate']);
            }
            var myChart5 = echarts.init($("#tab-5").get(0));
            option5 = {
                tooltip : {
                    trigger: 'axis',
                    axisPointer: {
                        type: 'cross',
                        label: {
                            backgroundColor: '#6a7985'
                        }
                    }
                },
                legend: {
                    data:['挂牌量','成交量']
                },
                grid: {
                    left: '4%',
                    right: '3%',
                    bottom: '6%',
                    containLabel: true
                },
                xAxis : [
                    {
                        type : 'category',
                        boundaryGap: false,
                        axisLabel: {
                            interval:0,
                            rotate:20
                        },
                        data: statsDate
                    }
                ],
                yAxis : [
                    {
                        type : 'value'
                    }
                ],
                series : [
                    {
                        name:'挂牌量',
                        type:'line',
                        smooth:true,
                        areaStyle: {normal: {}},
                        data:netNumber
                    },
                    {
                        name:'成交量',
                        type:'line',
                        smooth:true,
                        areaStyle: {normal: {}},
                        data:dealNumber
                    }
                ]
            };
            myChart5.setOption(option5);
            $(window).resize(function() {
                //重置容器高宽
                myChart5.resize;
            });
        }
    })
    // 配套评级详情百度地图API功能
    var communityName = $('#communityName').text();
    var point= $('#coord').val().split(',');
    var allmapPOI = new BMap.Map("allmapPOI");
    $('#allmapPOI').css('height',$('#POIList').height());
    allmapPOI.centerAndZoom(new BMap.Point(point[0],point[1]),15);
    allmapPOI.enableScrollWheelZoom();
     // allmapPOI.centerAndZoom(cityName, 15);
    var top_left_control = new BMap.ScaleControl({anchor: BMAP_ANCHOR_TOP_LEFT});// 左上角，添加比例尺
    var top_left_navigation = new BMap.NavigationControl();  //左上角，添加默认缩放平移控件
    allmapPOI.addControl(top_left_control);
    allmapPOI.addControl(top_left_navigation);
    var local = new BMap.LocalSearch(allmapPOI,{
        renderOptions:{map: allmapPOI, autoViewport:true,panel:"log"},pageCapacity: 6
    });
    // var options = {
    //     onSearchComplete: function(results){
    //         if (local.getStatus() == BMAP_STATUS_SUCCESS){
    //             // 判断状态是否正确
    //             var s = [];
    //             for (var i = 0; i < results.getCurrentNumPois(); i ++){
    //                 s.push(results.getPoi(i).title + ", " + results.getPoi(i).address);
    //             }
    //             document.getElementById("log").innerHTML = s.join("<br>");
    //         }
    //     }
    // };
    local.searchNearby("公交", new BMap.Point(point[0],point[1]));
    $('input').click(function(){
        var inputKW=$(this).val();
        // $(this).css('color','#3498DB');
        $(this).parent().parent().css('color','#3498DB');
        $(this).parent().parent().siblings().css('color','#6A6C6F');
        // $(this).siblings().css('color','#6A6C6F')
        local.searchNearby(inputKW, new BMap.Point(point[0],point[1]));
    })
})